/*=+--+=#=+--      UAS Severe Weather Simulation Softeware        --+=#=+--+=#*\
|          Copyright (C) 2013 Regents of the University of Colorado.           |
|                             All Rights Reserved.                             |
                                                                                
     This program is free software: you can redistribute it and/or modify       
     it under the terms of the GNU General Public License version 2 as          
     published by the Free Software Foundation.                                 
                                                                                
     This program is distributed in the hope that it will be useful,            
     but WITHOUT ANY WARRANTY; without even the implied warranty of             
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              
     GNU General Public License for more details.                               
                                                                                
     You should have received a copy of the GNU General Public License          
     along with this program.  If not, see <http://www.gnu.org/licenses/>.      
                                                                                
            Jack Elston                                                         
|           elstonj@colorado.edu                                               |
|                                                                              |
\*=+--+=#=+--                 --+=#=+--+=#=+--                    --+=#=+--+=#*/

#ifndef _SHM_STRUCT_GATEWAY
#define _SHM_STRUCT_GATEWAY

#include <pthread.h>
#include <inttypes.h>

#include "timeLib.h"

typedef struct _USBCANStatus_t {
	String state;
	uint16_t error;

	uint32_t tx;
	uint32_t rx;
	uint32_t dropped;

	String lib;
	uint16_t device;
	uint16_t speed;
	uint16_t apAddress;

	_USBCANStatus_t() {
		state = "Unknown";
		apAddress = 0;
		rx = 0;
		tx = 0;
		dropped = 0;
	}

} USBCANStatus_t;

typedef struct _UDPSocketStatus_t {
	String state;
	uint16_t error;

	uint32_t tx;
	uint32_t rx;
	uint32_t dropped;

	bool connected;
	String hostStr;
	String portStr;

	_UDPSocketStatus_t() {
		state = "Unknown";
		connected = false;
		hostStr = "";
		portStr = "";
		rx = 0;
		tx = 0;
		dropped = 0;
	}

} UDPSocketStatus_t;

typedef struct _servoStruct_t {
	uint16_t pulse;
	int16_t  angle;

	_servoStruct_t() {
		pulse = 0;
		angle = 0;
	}

} __attribute__ ((packed)) servoStruct_t;

typedef struct _confStruct_t {
	uint32_t time;
	uint32_t config;

	_confStruct_t() {
		time = 0;
	}

} __attribute__ ((packed)) confStruct_t;

typedef struct {
	servoStruct_t servos[16];
	confStruct_t  config;
} __attribute__ ((packed)) piccoloTx_t;

typedef struct {
	uint8_t month;
	uint8_t day;
	uint16_t year;
	uint8_t hour;
	uint8_t minute;
	uint8_t second;
	uint8_t frac_sec;
	int32_t lat;
	int32_t lon;
	uint32_t height;
	int16_t velocity[3];
} __attribute__ ((packed)) gpsStruct_t;

typedef struct _engineStruct_t {
	uint16_t lrpm;
	uint16_t rrpm;
	uint32_t time;

	_engineStruct_t() {
		time = 0;
	}

} __attribute__ ((packed)) engineStruct_t;

typedef struct {
	int16_t rates[3];
	int16_t accelerations[3];
	uint8_t sequence;
} __attribute__ ((packed)) imuStruct_t;

typedef struct {
	float dynamicp;
	float oat;
	float offsetp;
	float staticp;
} __attribute__ ((packed)) airStruct_t;

typedef struct {
	airStruct_t air;
	imuStruct_t imu;
	engineStruct_t engine;
	gpsStruct_t gps;
} __attribute__ ((packed)) piccoloRx_t;

typedef struct _system_t {
	bool  ready;
	float rate;
	TimeStamp time;

	_system_t() {
		ready = false;
		rate = 0.0;
		time.stamp();
	}

} system_t;

typedef struct {
	system_t servo;
	system_t conf;
	system_t air;
	system_t imu;
	system_t engine;
	system_t gps;
} packetStatus_t;

typedef struct _ShmStruct {
	pthread_mutex_t   mtx;
	USBCANStatus_t    usbcan_status;
	UDPSocketStatus_t udpsocket_status;
	packetStatus_t    packet_status;
	piccoloRx_t       piccolo_rx;
	piccoloTx_t       piccolo_tx;
} ShmStruct;

#endif
